<script>
import {
	finishOrder,
	deleteOrder,
	cancelOrder
} from "@/lib/api/orderServer/order";
export default {
	methods: {
		// 确认收货
		async confirmReceipt(orderId) {
			const showModalData = {
				title: "提示",
				content: "是否确认收货？",
				showCancel: true
			};
			await this.$api.showModal(showModalData);
			await this.onUpdateOrder(orderId, "1101");
		},

		async cancelOrder(orderId) {
			const showModalData = {
				title: "提示",
				content: "是否取消订单？",
				showCancel: true
			};
			await this.$api.showModal(showModalData);
			await this.onUpdateOrder(orderId, "1102");
		},

		// 删除订单
		async deleteOrder(orderId) {
			const showModalData = {
				title: "提示",
				content: "是否删除订单？",
				showCancel: true
			};
			await this.$api.showModal(showModalData);
			await this.onUpdateOrder(orderId, "1103");
		},

		/**
		 * 修改订单状态
		 * @param { String } orderId 订单id
		 * @param { String } status     // 1101: 确认收货, 2: 取消订单, 3: 删除订单
		 */
		async onUpdateOrder(orderId, status) {
			let editOrderApi = null;
			switch (status) {
				case "1101":
					editOrderApi = finishOrder;
					break;
				case "1102":
					editOrderApi = cancelOrder;
					break;
				case "1103":
					editOrderApi = deleteOrder;
					break;

				default:
					break;
			}

			return editOrderApi({ orderId }).then(res => {
				this.$api.showToast(res.resultInfo || "操作成功");
			});
		},

		// 查看物流
		queryLogistisc(singleNumber) {
			uni.navigateTo({
				url: `/pages/user/pages/logistics/logistics?singleNumber=${singleNumber}`
			});
		},
		/**
     * 操作按钮展示
     * @param { Number } btnType // 按钮类型 1: 查看物流; 2: 待付款 (取消订单 or 立即付款); 3: 确认收货; 4: 删除订单 or 再次购买 5：出示核销码
     * @param { Number } status  // 100待付款，150拼团已付款待成团，250待到店，200待发货，300已发货，400已完成，500已取消
	 * @param { Number } orderType  // 10线上订单，15到店自取，20收银台收款
     */
		viewBtn(status, btnType, orderType) {
			let isView = false;
			//线上订单
			if (orderType === 10) {
				switch (btnType) {
					case 1:
						if (status === 300 || status === 400) {
							isView = true;
						}
						break;
					case 2:
						if (status === 100) {
							isView = true;
						}
						break;
					case 3:
						if (status === 300) {
							isView = true;
						}
						break;
					case 4:
						if (status === 400 || status === 500) {
							isView = true;
						}
						break;
					case 5:
						isView = false;
						break;
					default:
						break;
				}
			//到店自取
			} else if (orderType === 15) {
				switch (btnType) {
					case 1:
						isView = false;
						break;
					case 2:
						if (status === 100) {
							isView = true;
						}
						break;
					case 3:
						isView = false;
						break;
					case 4:
						if (status === 400 || status === 500) {
							isView = true;
						}
						break;
					case 5:
						if (status === 250 ) {
							isView = true;
						}
						break;
					default:
						break;
				}
			//收银台收款
			} else if (orderType === 20) {
				switch (btnType) {
					case 1:
						isView = false;
						break;
					case 2:
						isView = false;
						break;
					case 3:
						isView = false;
						break;
					case 4:
						if (status === 400 || status === 500) {
							isView = true;
						}
						break;
					case 5:
						isView = false;
						break;
					default:
						break;
				}
			} else {
				switch (btnType) {
					case 1:
						if (status === 300 || status === 400) {
							isView = true;
						}
						break;
					case 2:
						if (status === 100) {
							isView = true;
						}
						break;
					case 3:
						if (status === 300) {
							isView = true;
						}
						break;
					case 4:
						if (status === 400 || status === 500) {
							isView = true;
						}
						break;
					case 5:
						isView = false;
						break;
					default:
						break;
				}
			}
			return isView;
		}
	}
};
</script>